package com.example.myui.activity;

import android.Manifest;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.myui.R;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class GlideCacheActivity extends AppCompatActivity {

    private ArrayList list;
    private String TAG = GlideCacheActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide_cache);
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)
                .subscribe(new Observer<Permission>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Permission permission) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        initData();
        final ImageView imageview1 = findViewById(R.id.image1);
        final ImageView imageview2 = findViewById(R.id.image2);
        final ImageView imageview3 = findViewById(R.id.image3);
        final Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
        final Button btn3 = findViewById(R.id.btn3);

        RecyclerView recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(GlideCacheActivity.this, LinearLayoutManager.VERTICAL, false));

        Myadapter adapter = new Myadapter();
        recyclerview.setAdapter(adapter);


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(GlideCacheActivity.this)
                        .load(list.get(0))
                        .into(imageview1);
            }
        });


        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(GlideCacheActivity.this)
                        .load(list.get(1))
                        .into(imageview2);

                Log.i(TAG, "onClick: " + imageview3.getWidth());
                Log.i(TAG, "onClick: " + imageview3.getMeasuredWidth());
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                long size= 0;
//                String path = Environment.getExternalStorageDirectory() + "/aaaaa";
//                File[] list = new File(path).listFiles();
//                for (File file : list) {
//                    Log.i(TAG, "onClick: " + file.getName());
//                }
//
//                Glide.with(MainActivity.this)
//                        .load(list[1])
//                        .into(imageview3);
                String path = Environment.getExternalStorageDirectory() + "/aaaaa";
                File file = new File(path);
                long folderSize = getFolderSize(file);
                btn3.setText(folderSize + "");

            }
        });

    }

    private long getFolderSize(File file) {
        long size = 0;
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                size = size + getFolderSize(file1);
            } else {
                size = size + file1.length();
            }
        }
        return size;
    }

    private void initData() {
        // 图片url在百度随便搜的
        list = new ArrayList<>();
//        list.add("http://img3.imgtn.bdimg.com/it/u=4139437186,74667515&fm=21&gp=0.jpg");
//        list.add("http://img2.niutuku.com/1312/0831/0831-niutuku.com-28071.jpg");
//        list.add("http://www.pp3.cn/uploads/201408/1406686270117.jpg");
        list.add("http://img2.niutuku.com/desk/1208/2148/ntk-2148-41003.jpg");
        list.add("http://img.dota2.com.cn/dota2/01/4c/014c4eb20ae9cda9213d64d02a18f5261463732426.jpg");
        list.add("http://img.dota2.com.cn/dota2/d3/5a/d35af02410b2ab10a4aa15d23795a0d11463732396.jpg");
        list.add("http://img2.niutuku.com/desk/1208/2148/ntk-2148-41003.jpg");
        list.add("http://img.dota2.com.cn/dota2/01/4c/014c4eb20ae9cda9213d64d02a18f5261463732426.jpg");
        list.add("http://img.dota2.com.cn/dota2/d3/5a/d35af02410b2ab10a4aa15d23795a0d11463732396.jpg");
        list.add("http://img2.niutuku.com/desk/1208/2148/ntk-2148-41003.jpg");
        list.add("http://img.dota2.com.cn/dota2/01/4c/014c4eb20ae9cda9213d64d02a18f5261463732426.jpg");
        list.add("http://img.dota2.com.cn/dota2/d3/5a/d35af02410b2ab10a4aa15d23795a0d11463732396.jpg");
        list.add("http://img2.niutuku.com/desk/1208/2148/ntk-2148-41003.jpg");
        list.add("http://img.dota2.com.cn/dota2/01/4c/014c4eb20ae9cda9213d64d02a18f5261463732426.jpg");
        list.add("http://img.dota2.com.cn/dota2/d3/5a/d35af02410b2ab10a4aa15d23795a0d11463732396.jpg");
        list.add("http://img2.niutuku.com/desk/1208/2148/ntk-2148-41003.jpg");
        list.add("http://img.dota2.com.cn/dota2/01/4c/014c4eb20ae9cda9213d64d02a18f5261463732426.jpg");
        list.add("http://img.dota2.com.cn/dota2/d3/5a/d35af02410b2ab10a4aa15d23795a0d11463732396.jpg");

    }

    private class Myadapter extends RecyclerView.Adapter<Myadapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(GlideCacheActivity.this).inflate(R.layout.item_image_text, parent, false);
            Holder holder = new Holder(inflate);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            Glide.with(GlideCacheActivity.this)
                    .load(list.get(position))
                    .into(holder.imageView);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class Holder extends RecyclerView.ViewHolder {
            private ImageView imageView;

            public Holder(View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.imageview);
            }
        }
    }
}
